Dependency Injection (DI) একটি ডিজাইন প্যাটার্ন যা অ্যাপ্লিকেশনগুলিতে Loose Coupling অর্জন করতে সাহায্য করে। এটি আপনাকে কম্পোনেন্টগুলির মধ্যে নির্ভরশীলতাগুলি বাইরের উৎস থেকে ইনজেক্ট করতে দেয়, যার ফলে কোডের রক্ষণাবেক্ষণ এবং পরীক্ষা করা সহজ হয়। .NET MAUI তে DI ব্যবহারের মাধ্যমে, আপনি কম্পোনেন্টগুলির মধ্যে নির্ভরশীলতাগুলি ম্যানেজ করতে পারেন এবং আপনার অ্যাপ্লিকেশনের কাঠামোকে আরও পরিষ্কার এবং নমনীয় করতে পারেন।
.NET MAUI অ্যাপ্লিকেশনগুলিতে Dependency Injection (DI) সিস্টেমটি তৈরি করার জন্য .NET Core থেকে আগত IServiceProvider এর উপর নির্ভরশীল। .NET MAUI তে DI সেটআপ করা সহজ এবং এটি কম্পোনেন্ট এবং সার্ভিসগুলির মধ্যে নির্ভরশীলতা প্রদান করতে সাহায্য করে।
.NET MAUI তে DI ব্যবহারের জন্য নিচের পদক্ষেপগুলি অনুসরণ করতে হবে:
প্রথমত, অ্যাপ্লিকেশনের App.xaml.cs
ফাইলে আপনাকে Dependency Injection Container সেটআপ করতে হবে।
App.xaml.cs:
using Microsoft.Extensions.DependencyInjection;
public partial class App : Application
{
public App()
{
InitializeComponent();
// Dependency Injection Container তৈরি করা
var services = new ServiceCollection();
// আপনার সার্ভিস রেজিস্টার করুন
services.AddSingleton<IMyService, MyService>();
// MainPage সেটআপ করা
var serviceProvider = services.BuildServiceProvider();
MainPage = serviceProvider.GetRequiredService<MainPage>();
}
}
এখানে, আমরা ServiceCollection
ব্যবহার করে DI কনটেইনার তৈরি করেছি এবং তারপর AddSingleton
পদ্ধতিতে আমাদের IMyService
এবং MyService
রেজিস্টার করেছি। এর মাধ্যমে আমরা সার্ভিসটি অ্যাপ্লিকেশনের lifecycle এর সাথে একযোগে ব্যবহার করতে পারব।
DI ব্যবহারের জন্য আপনাকে প্রথমে একটি Service Interface এবং তার সাথে সম্পর্কিত Implementation তৈরি করতে হবে।
IMyService.cs:
public interface IMyService
{
string GetGreeting();
}
MyService.cs:
public class MyService : IMyService
{
public string GetGreeting()
{
return "Hello from MyService!";
}
}
এখানে, IMyService
একটি ইন্টারফেস যা GetGreeting()
মেথড সংজ্ঞায়িত করেছে, এবং MyService
ক্লাস এই ইন্টারফেসটি বাস্তবায়ন করেছে।
এখন, আপনি DI দ্বারা ইনজেক্ট করা সার্ভিসটি UI বা অন্য কোনো কম্পোনেন্টে ব্যবহার করতে পারেন। উদাহরণস্বরূপ, MainPage.xaml.cs
ফাইলে IMyService
ইনজেক্ট করা যাবে।
MainPage.xaml.cs:
public partial class MainPage : ContentPage
{
private readonly IMyService _myService;
// Constructor Injection
public MainPage(IMyService myService)
{
InitializeComponent();
_myService = myService;
// Service থেকে ডেটা গ্রহণ করা
var greeting = _myService.GetGreeting();
GreetingLabel.Text = greeting;
}
}
এখানে, MainPage
ক্লাসের কনস্ট্রাক্টরে IMyService
ইনজেক্ট করা হয়েছে এবং _myService.GetGreeting()
ব্যবহার করে সার্ভিসের মাধ্যমে গ্রীটিং বার্তা আনা হয়েছে।
এখন, আপনি XAML ফাইলে প্রাপ্ত ডেটাটি প্রদর্শন করতে পারেন।
MainPage.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MyMauiApp.MainPage">
<StackLayout>
<Label x:Name="GreetingLabel"
Text="Loading..."
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
এখানে, GreetingLabel টেক্সট আউটপুট দেখাবে, যেটি সার্ভিস থেকে প্রাপ্ত গ্রীটিং বার্তা হবে।
Scoped Example:
services.AddScoped<IMyService, MyService>();
Transient Example:
services.AddTransient<IMyService, MyService>();
DI সাধারণত MVVM (Model-View-ViewModel) প্যাটার্নের সাথে ব্যবহার করা হয়, যেখানে ViewModel লজিক এবং ডেটা প্রক্রিয়া পরিচালনা করে, এবং View UI এর উপস্থাপনা করে। .NET MAUI তে DI ব্যবহারের মাধ্যমে ViewModel কে View এ ইনজেক্ট করা যায় এবং অ্যাপ্লিকেশনের লজিক উন্নত করা হয়।
MainPageViewModel.cs:
public class MainPageViewModel
{
private readonly IMyService _myService;
// Constructor Injection
public MainPageViewModel(IMyService myService)
{
_myService = myService;
}
public string Greeting => _myService.GetGreeting();
}
MainPage.xaml.cs:
public MainPage()
{
InitializeComponent();
BindingContext = new MainPageViewModel(DependencyService.Get<IMyService>());
}
Dependency Injection (DI) হল একটি ডিজাইন প্যাটার্ন যা প্রোগ্রামিংয়ে ব্যবহার করা হয়, যেখানে একটি ক্লাস তার নির্ভরশীলতাগুলো (dependencies) সরাসরি তৈরি না করে, বরং বাইরে থেকে ইনজেক্ট করা হয়। এটি মূলত একটি অবজেক্টকে তার প্রয়োজনীয় অবজেক্ট বা সেবা প্রদান করার একটি পদ্ধতি।
Constructor Injection:
public class ServiceConsumer
{
private readonly IService _service;
public ServiceConsumer(IService service)
{
_service = service;
}
public void PerformAction()
{
_service.Execute();
}
}
Property Injection:
public class ServiceConsumer
{
public IService Service { get; set; }
public void PerformAction()
{
Service.Execute();
}
}
Method Injection:
public class ServiceConsumer
{
public void PerformAction(IService service)
{
service.Execute();
}
}
// IService Interface
public interface IService
{
void Execute();
}
// Service Implementation
public class MyService : IService
{
public void Execute()
{
Console.WriteLine("Service Executed");
}
}
// Consumer Class
public class ServiceConsumer
{
private readonly IService _service;
// Constructor Injection
public ServiceConsumer(IService service)
{
_service = service;
}
public void PerformAction()
{
_service.Execute();
}
}
// DI Container Setup (In .NET MAUI or ASP.NET Core)
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IService, MyService>(); // Register service
services.AddTransient<ServiceConsumer>(); // Register consumer
}
}
এখানে ServiceConsumer ক্লাসের কনস্ট্রাক্টরের মাধ্যমে IService ইনজেক্ট করা হয়েছে, এবং MyService সেবা ব্যবহার করা হচ্ছে।
Dependency Injection (DI) একটি শক্তিশালী ডিজাইন প্যাটার্ন যা কোডের রক্ষণাবেক্ষণ, টেস্টিং এবং সম্প্রসারণকে সহজ করে তোলে। এটি ক্লাসের মধ্যে নির্ভরশীলতা ইনজেক্ট করে, যাতে কোডের মধ্যে দৃঢ় সংযুক্তি কমে এবং কোড আরও নমনীয়, পুনঃব্যবহারযোগ্য এবং টেস্টযোগ্য হয়। .NET এবং অন্যান্য আধুনিক ফ্রেমওয়ার্কগুলিতে DI ব্যবহৃত হয় যাতে অ্যাপ্লিকেশনগুলো আরও স্থিতিশীল, দক্ষ এবং সহজে বজায় রাখা যায়।
Dependency Injection (DI) হল একটি ডিজাইন প্যাটার্ন যা অ্যাপ্লিকেশনের কম্পোনেন্টগুলির মধ্যে ডিপেনডেন্সি বা নির্ভরতা পরিচালনার জন্য ব্যবহৃত হয়। .NET MAUI এ Built-in Dependency Injection ব্যবহার করে অ্যাপ্লিকেশনগুলির মধ্যে সার্ভিস এবং ডিপেনডেন্সি খুব সহজে ইনজেক্ট করা যায়।
.NET MAUI এ DI ব্যবহারের মাধ্যমে আপনি অ্যাপ্লিকেশনের বিভিন্ন সার্ভিস এবং ডিপেনডেন্সি গুলি নিবন্ধিত (register) করতে পারেন এবং সেগুলি ক্লাসে ইনজেক্ট (inject) করতে পারেন, যাতে অ্যাপ্লিকেশন কোড পরিষ্কার, ম্যানেজেবল এবং টেস্টেবল হয়।
প্রথমে, App.xaml.cs বা App.cs ফাইলে ডিপেনডেন্সি ইনজেকশন কনফিগার করতে হবে। এখানে, ConfigureServices মেথড ব্যবহার করে অ্যাপ্লিকেশনের জন্য সার্ভিস রেজিস্ট্রেশন করা হয়।
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Essentials;
using MauiApp1.Services;
namespace MauiApp1
{
public partial class App : Application
{
public App(IServiceProvider serviceProvider)
{
InitializeComponent();
// Initialize main page with DI
MainPage = serviceProvider.GetRequiredService<MainPage>();
}
// Register services and dependency injection
protected override void OnStart()
{
var services = new ServiceCollection();
// Register services for DI
services.AddSingleton<MainPage>();
services.AddSingleton<IDataService, DataService>();
// Build the service provider
var serviceProvider = services.BuildServiceProvider();
// Set the AppShell as the main page
MainPage = serviceProvider.GetRequiredService<MainPage>();
}
}
}
এখানে:
এখন, আপনি যে সার্ভিসটি ইনজেক্ট করতে চান তা তৈরি করতে হবে। নিচে একটি উদাহরণ হিসেবে IDataService এবং তার DataService ইমপ্লিমেন্টেশন দেওয়া হল।
public interface IDataService
{
string GetData();
}
public class DataService : IDataService
{
public string GetData()
{
return "Hello from the Data Service!";
}
}
এখানে:
এখন, আপনার MainPage.xaml.cs বা যেকোনো পেজে আপনি IDataService সার্ভিসটি ইনজেক্ট করতে পারবেন।
using MauiApp1.Services;
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
private readonly IDataService _dataService;
public MainPage(IDataService dataService)
{
InitializeComponent();
_dataService = dataService;
// Use the injected service to get data
DataLabel.Text = _dataService.GetData();
}
}
}
এখানে:
এখন, MainPage.xaml ফাইলে একটি লেবেল তৈরি করা হয়েছে যেখানে ডেটা প্রদর্শিত হবে।
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MauiApp1.MainPage">
<StackLayout Padding="20">
<Label x:Name="DataLabel" FontSize="24" />
</StackLayout>
</ContentPage>
এখানে:
এখন, সবকিছু একসাথে সাজানো হল।
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Controls;
using MauiApp1.Services;
namespace MauiApp1
{
public partial class App : Application
{
public App(IServiceProvider serviceProvider)
{
InitializeComponent();
// Initialize main page with DI
MainPage = serviceProvider.GetRequiredService<MainPage>();
}
// Register services and dependency injection
protected override void OnStart()
{
var services = new ServiceCollection();
// Register services for DI
services.AddSingleton<MainPage>();
services.AddSingleton<IDataService, DataService>();
// Build the service provider
var serviceProvider = services.BuildServiceProvider();
// Set the AppShell as the main page
MainPage = serviceProvider.GetRequiredService<MainPage>();
}
}
}
এখানে:
Services এবং Repositories হল .NET MAUI বা অন্য কোন .NET অ্যাপ্লিকেশনে ডেটা অ্যাক্সেস এবং লজিক ব্যবস্থাপনা করতে ব্যবহৃত সাধারণ ডিজাইন প্যাটার্ন। তারা অ্যাপ্লিকেশনের কোডকে আরও পরিষ্কার এবং টেস্টযোগ্য করতে সাহায্য করে, যাতে বিজনেস লজিক এবং ডেটা অ্যাক্সেস আলাদা রাখা যায়।
Service একটি ক্লাস যা অ্যাপ্লিকেশনের বিজনেস লজিক ধারণ করে। এটি সাধারণত ডেটা প্রসেসিং, ব্যবহারকারী যাচাই, বা অন্য কোন বিজনেস প্রক্রিয়া সম্পাদন করতে ব্যবহৃত হয়। Services সাধারণত অ্যাপ্লিকেশনের একাধিক জায়গায় ব্যবহৃত হয় এবং কন্ট্রোলারের সাথে ইন্টারঅ্যাক্ট করে।
public interface ICustomerService
{
Task<List<Customer>> GetCustomersAsync();
Task<Customer> GetCustomerByIdAsync(int id);
}
public class CustomerService : ICustomerService
{
private readonly ICustomerRepository _customerRepository;
// Dependency Injection of Repository
public CustomerService(ICustomerRepository customerRepository)
{
_customerRepository = customerRepository;
}
// Get all customers
public async Task<List<Customer>> GetCustomersAsync()
{
return await _customerRepository.GetAllCustomersAsync();
}
// Get customer by id
public async Task<Customer> GetCustomerByIdAsync(int id)
{
return await _customerRepository.GetCustomerByIdAsync(id);
}
}
এখানে:
Repository হল একটি ক্লাস বা ইন্টারফেস যা ডেটাবেস বা অন্যান্য ডেটা সোর্সের সাথে যোগাযোগ করে এবং ডেটা অ্যাক্সেস অপারেশনগুলি সম্পাদন করে। এটি ডেটা অ্যাক্সেস লজিককে পরিষ্কার এবং পুনঃব্যবহারযোগ্য রাখতে সাহায্য করে।
public interface ICustomerRepository
{
Task<List<Customer>> GetAllCustomersAsync();
Task<Customer> GetCustomerByIdAsync(int id);
}
public class CustomerRepository : ICustomerRepository
{
private readonly DbContext _context;
// Constructor injection for DbContext
public CustomerRepository(DbContext context)
{
_context = context;
}
// Get all customers
public async Task<List<Customer>> GetAllCustomersAsync()
{
return await _context.Customers.ToListAsync();
}
// Get customer by id
public async Task<Customer> GetCustomerByIdAsync(int id)
{
return await _context.Customers.FindAsync(id);
}
}
এখানে:
.NET MAUI অ্যাপ্লিকেশনে Dependency Injection (DI) ব্যবহার করে Services এবং Repositories সরবরাহ করা হয়। .NET MAUI তে DI কনফিগার করা সহজ এবং এটি অ্যাপ্লিকেশনের বিভিন্ন অংশে পরিষ্কারভাবে ডিপেনডেন্সি ইনজেকশন করতে সাহায্য করে।
public partial class App : Application
{
public App()
{
InitializeComponent();
// Registering services and repositories
DependencyService.Register<ICustomerService, CustomerService>();
DependencyService.Register<ICustomerRepository, CustomerRepository>();
MainPage = new MainPage();
}
}
এখানে:
MainPage.xaml.cs এর মধ্যে CustomerService ইনজেক্ট করে ব্যবহার করা:
public partial class MainPage : ContentPage
{
private readonly ICustomerService _customerService;
public MainPage(ICustomerService customerService)
{
InitializeComponent();
_customerService = customerService;
}
protected override async void OnAppearing()
{
base.OnAppearing();
// Get all customers
var customers = await _customerService.GetCustomersAsync();
// Use customers data here
}
}
এখানে:
Dependency Injection (DI) একটি ডিজাইন প্যাটার্ন যা অবজেক্টের নির্ভরশীলতা বাইরের উৎস থেকে ইনজেক্ট করার মাধ্যমে কোডের মডুলারিটি এবং টেস্টেবিলিটি বৃদ্ধি করে। ViewModelLocator একটি সাধারণ কৌশল যা MVVM (Model-View-ViewModel) ডিজাইন প্যাটার্নে ডিপেন্ডেন্সি ইনজেকশন পরিচালনা করতে ব্যবহৃত হয়। .NET MAUI এ DI কার্যকর করার জন্য ViewModelLocator খুবই সহায়ক, কারণ এটি আপনাকে ViewModel গুলোর জন্য ডিপেন্ডেন্সি সহজভাবে ইনজেক্ট করতে সহায়তা করে এবং ViewModel গুলি কমপ্লেক্সলি ম্যানেজ করার সুযোগ দেয়।
আমরা একটি সাধারণ উদাহরণ তৈরি করবো যেখানে আমরা MainPageViewModel
এবং ApiService
ক্লাসের মধ্যে ডিপেন্ডেন্সি ইনজেকশন ব্যবহার করব।
প্রথমে আমরা একটি সেবা (service) ক্লাস তৈরি করি যা ডাটা রিটার্ন করবে। এখানে, আমরা একটি ApiService
ক্লাস তৈরি করেছি।
public class ApiService
{
public string GetData()
{
return "Hello from ApiService!";
}
}
public class MainPageViewModel : BindableObject
{
private readonly ApiService _apiService;
private string _message;
public string Message
{
get => _message;
set
{
_message = value;
OnPropertyChanged();
}
}
// Dependency Injection via constructor
public MainPageViewModel(ApiService apiService)
{
_apiService = apiService;
LoadData();
}
private void LoadData()
{
Message = _apiService.GetData();
}
}
এখানে MainPageViewModel
কন্সট্রাক্টরে ApiService
ইনজেক্ট করা হয়েছে, যা ডিপেন্ডেন্সি ইনজেকশনের মাধ্যমে সরবরাহ করা হবে।
এখন আমরা একটি ViewModelLocator
ক্লাস তৈরি করবো যা আমাদের ViewModel-এ ডিপেন্ডেন্সি ইনজেক্ট করবে।
public class ViewModelLocator
{
public MainPageViewModel MainPageViewModel { get; }
public ViewModelLocator()
{
// Dependency Injection
var apiService = new ApiService();
MainPageViewModel = new MainPageViewModel(apiService);
}
}
এখানে ViewModelLocator
ক্লাসের কন্সট্রাক্টর থেকে আমরা ApiService
তৈরি করেছি এবং তা MainPageViewModel
এ ইনজেক্ট করেছি। এর মাধ্যমে MainPageViewModel
-এর ডিপেন্ডেন্সি ইনজেক্ট হয়েছে এবং আমরা এটি ViewModelLocator থেকে এক্সেস করতে পারব।
এখন আমরা App.xaml.cs ফাইলে ViewModelLocator কে রেজিস্টার করব এবং আমাদের MainPage
-এ সেট করে দেব।
public partial class App : Application
{
public static ViewModelLocator ViewModelLocator { get; } = new ViewModelLocator();
public App()
{
InitializeComponent();
// Setting the BindingContext of MainPage to ViewModel
MainPage = new MainPage
{
BindingContext = ViewModelLocator.MainPageViewModel
};
}
}
এখানে আমরা ViewModelLocator কে App.xaml.cs
ফাইলে স্ট্যাটিকভাবে রেজিস্টার করেছি এবং MainPage
-এর BindingContext
হিসাবে সেট করেছি।
এখন আমরা MainPage.xaml
-এ ViewModel কে বাইন্ড করবো এবং UI-তে ডাটা প্রদর্শন করবো।
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MAUIApp.MainPage">
<StackLayout Padding="10">
<Label Text="{Binding Message}"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
এখানে, আমরা Message
প্রপার্টি কে MainPageViewModel
এর BindingContext
হিসেবে সেট করেছি এবং Data Binding এর মাধ্যমে সেটি UI তে দেখাচ্ছি।
এখন, আপনার অ্যাপটি রান করলে, MainPage-এ Message
প্রপার্টি থেকে ডাটা দেখাবে, যা ApiService
থেকে এসেছে। এতে দেখা যাবে, "Hello from ApiService!" এই টেক্সট।
এটি .NET MAUI অ্যাপ্লিকেশনে DI ব্যবহারের একটি কার্যকর কৌশল।
Read more